//
//  FWAuthUser.m
//  FWShare
//
//  Created by 范文青 on 14-6-14.
//  Copyright (c) 2014年 fwq. All rights reserved.
//

#import "FWAuthUser.h"
#import "SSKeychainQuery.h"
#import "FWShareValues.h"
#import "NSDate+transform.h"
NSString  *const kWTEUserInfoSaveKey = @"kWTEUserInfoSaveKey";
@implementation FWAuthUser
- (id)getValueForObject:(id)obj key:(NSString *)key{
    if ([key isEqualToString:@"token"]) {
        return [obj dictionaryRepresentation];
    }
    return nil;
}
- (void)setValue:(id)value forKey:(NSString *)key
{
    if ([key isEqualToString:@"token"]) {
        value = [[FWAccessToken alloc] initWithDictionary:value];
    }
    [super setValue:value forKey:key];
}
+(FWAuthUser *)instanceSina:(NSDictionary *)dic
{
    FWAuthUser  *user = [FWAuthUser new];
    user.avatar = [dic objectForKey:@"profile_image_url"];
    user.avatar_large = [dic objectForKey:@"avatar_large"];
    user.nick = [dic objectForKey:@"name"];
    user.screen_nick = [dic objectForKey:@"screen_name"];
    
    user.gender = [dic objectForKey:@"gender"];
    user.udescription = [dic objectForKey:@"description"];
    double  create_at= [[NSDate sinaDate:[dic objectForKey:@"created_at"]] timeIntervalSince1970];
    user.created_at = [NSString stringWithFormat:@"%.0f",create_at];
    user.uid = [[dic objectForKey:@"id"] description];
    user.media_type = SINA_WEIBO;
    return user;
}
+(FWAuthUser *)instanceQQWeiBo:(NSDictionary *)dic
{
    dic = [dic objectForKey:@"data"];
    FWAuthUser  *user = [FWAuthUser new];
    user.avatar = [dic objectForKey:@"head"];
    user.avatar_large = [dic objectForKey:@"head"];
    user.nick = [dic objectForKey:@"name"];
    user.screen_nick = [dic objectForKey:@"nick"];
    
    int sex = [[dic objectForKey:@"sex"] intValue];
    if (sex == 0) {
        user.gender = @"n";
    }else if (sex == 1){
        user.gender = @"m";
    }else if (sex == 2){
        user.gender = @"n";
    }
    user.udescription = [dic objectForKey:@"introduction"];
    user.created_at = [[dic objectForKey:@"regtime"] description];
    
    user.uid = [[dic objectForKey:@"openid"] description];
    user.media_type = QQ_WEIBO;
    return user;
}
+(FWAuthUser *)instanceQQHL:(NSDictionary *)dic
{
    FWAuthUser  *user = [FWAuthUser new];
    user.avatar = [dic objectForKey:@"figureurl"];
    user.avatar_large = [dic objectForKey:@"figureurl_2"];
    user.nick = [dic objectForKey:@"nickname"];
    user.screen_nick = [dic objectForKey:@"nickname"];
    
    NSString *sex = [dic objectForKey:@"gender"];
    if ([sex isEqualToString:@"女"]) {
        user.gender = @"f";
    }else{
        user.gender = @"m";
    }
    
    user.media_type = QQ_HULIAN;
    return user;
}
- (void)save
{
    SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
	query.service = kWTEUserInfoSaveKey;
	query.account = self.media_type;
	query.passwordObject = [self dictionaryRepresentation];
    NSError  *error;
    [query save:&error];
}
- (BOOL)isValid
{
    double timespanNow = [[NSDate date] timeIntervalSince1970];
    double timespanExpire = [[_token expires_in] doubleValue];
    if (timespanNow >= timespanExpire) {
        return NO;
    }
    return YES;
}
+(FWAuthUser *)userByMediaType:(NSString *)mediaType
{
    SSKeychainQuery *query = [[SSKeychainQuery alloc] init];
    query.service = kWTEUserInfoSaveKey;
    query.account = mediaType;
    query.passwordObject = nil;
    NSError  *error;
    [query fetch:&error];
    if (query.passwordObject != nil) {
        FWAuthUser *info = [[FWAuthUser alloc] initWithDictionary:(NSDictionary *)query.passwordObject];
        if ([info isValid]) {
            return info;
        }
    }
    return nil;
}
@end
